#!/bin/bash
set -e

noticeln() { echo -e -n "\033[01;32m$@\n\033[0m"; }

start () {
    ip=$(ssh ecs-hk "curl -s ip.me")
    noticeln "Start proxy server: $ip"
    ssh ecs-hk "systemctl start squid"
    export http_proxy=http://$ip:8081
    export https_proxy=http://$ip:8081

    if curl -Ss -m 1 google.com -o /dev/null; then
        noticeln "proxy success"
        # export PS1='[\u@\h-proxy \W]\$'
        exec bash
    else
        noticeln "proxy failed"
    fi
}

stop () {
    ip=$(ssh ecs-hk "curl -s ip.me")
    noticeln "Stop proxy server: $ip"
    ssh ecs-hk "systemctl stop squid"
    unset http_proxy
    unset https_proxy
}

status () {
    ip=$(ssh ecs-hk "curl -s ip.me")
    noticeln "proxy server status: $ip"
    ssh ecs-hk "systemctl status squid"
}

help () {
    echo "proxy [start|stop|status]"
}

case $1 in
    start ) start ;;
    stop ) stop ;;
    status ) status ;;
    * ) help ;;
esac

# set secgroup


